package com.tanhua.admin.listener;

import com.itheima.dubbo.api.mongo.MovementApi;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.autoconfig.template.AliyunGreenTemplate;
import com.tanhua.model.domain.Log;
import com.tanhua.model.mongo.Movement;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class MovementListener {

   @DubboReference
    private MovementApi movementApi;

   @Autowired
   private AliyunGreenTemplate greenTemplate;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "tanhua.audit.queue"),
    exchange = @Exchange(name = "tanhua.audit.exchange",type = ExchangeTypes.TOPIC),key = "audit.movement"))
    public void checkMovement(String movementId) throws Exception {
        log.info("======开始审核朋友圈===========>"+movementId);
        //1. 根据id查找对应的movement
        Movement movement = movementApi.findById(movementId);

        //2. 获取movement的文字与图片，调用阿里云进行内容审核
        String textContent = movement.getTextContent();
        List<String> medias = movement.getMedias();
        String contentResult = greenTemplate.greenTextScan(textContent).get("suggestion");
        String imageResult = (String) greenTemplate.imageScan(medias).get("suggestion");
        int state = 0;
        if("pass".equals(contentResult)&&"pass".equals(imageResult)){
            //4. 如果文字与图片都通过了，审核状态是1
            state=1;
        }else{
            //3. 审核如果文字或者图片任何一方出问题状态都修改为2，
            state=2;
        }

        movementApi.updateMovement(movementId,state);

    }
}
